import { ref } from 'vue'
import app from '@/app'
import { config, tax, signer, addSigner, client, addClient, updateClient } from '@/api/provider/fiskaly'
import base from '@/app/model'
import ticket from './ticket'
import Print from '../common/print'

// 继承总模型
const model = new base;

model.get = () => {
  return new Promise((resolve, jeject) => {
    config().then(res => {
      if(res) {
        model.setData(res)
        return resolve(res)
      }
      jeject(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

model.update = (data) => {
  return new Promise((resolve, jeject) => {
    config(data).then(res => {
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      app.ui.notify('warning', app.lang.t('common.warning'), res.message)
      jeject(res)
    })
  })
}

model.tax = ref(false)
model.getTax = () => {
  return new Promise((resolve, jeject) => {
    tax().then(res => {
      model.tax.value = res
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

model.signer = ref([])
model.signerLoading = ref(false)
model.getSigner = () => {
  return new Promise((resolve, jeject) => {
    model.signerLoading.value = true
    signer().then(res => {
      model.signer.value = res
      resolve(res)
    }).catch(res => {
      jeject(res)
    }).finally(() => model.signerLoading.value = false)
  })
}
model.addSigner = () => {
  return new Promise((resolve, jeject) => {
    addSigner().then(res => {
      app.ui.message('success', 'create')
      model.getSigner()
      resolve(res)
    }).catch(res => {
      app.ui.message('warning', 'signer.' + res.message, 'Fiskaly')
      jeject(res)
    })
  })
}

model.client = ref([])
model.clientLoading = ref(false)
model.getClient = () => {
  return new Promise((resolve, jeject) => {
    model.clientLoading.value = true
    client().then(res => {
      model.client.value = res
      resolve(res)
    }).catch(res => {
      jeject(res)
    }).finally(() => model.clientLoading.value = false)
  })
}
model.addClient = () => {
  return new Promise((resolve, jeject) => {
    addClient().then(res => {
      app.ui.message('success', 'create')
      model.getClient()
      resolve(res)
    }).catch(res => {
      app.ui.message('warning', 'client.' + res.message, 'Fiskaly')
      jeject(res)
    })
  })
}
model.updateClient = (id) => {
  return new Promise((resolve, jeject) => {
    updateClient(id).then(res => {
      model.get()
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

// 查询自动模型
model.autoOk = false
model.auto = async () => {
  if(model.autoOk) return

  // 首次加载配置
  if(!model.getData().api_key) {
    await model.get()
  }

  // 判断是否自动
  if(!model.getData().pos_auto) return

  // 判断是否配置客户端
  if(!model.getData().client_id) {
    return app.ui.notify('warning', 'Fiskaly', app.lang.t('Fiskaly.notify.client'))
  }

  // 加载一次税务信息，检查
  model.getTax().then(() => {
    // 标识成功，将永不加载
    model.autoOk = true
    app.ui.notify('success', 'Fiskaly', app.lang.t('Fiskaly.notify.auto'))
    // 注册事件
    app.plugin.register('checkout_over', id => model.autoUpload(id))
  })
}

// 自动上传发票
model.autoUpload = (id) => {
  app.ui.notify('info', 'Fiskaly', app.lang.t('Fiskaly.notify.auto_upload'))
  ticket.create(id).then(data => {
    // 判断是否自动打印
    if(model.getData().pos_auto > 1) {
      Print.load(data.data)
    }
  })
}

export default model